﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Imaginecup.shareData;

namespace Imaginecup.util.vertex2D
{
    public class Vertex2DBuffer
    {
        private VertexPositionTexture[] m_VertexArray ;
        private int m_Size;
       
        public Vertex2DBuffer(int size)
        {
            m_VertexArray = new VertexPositionTexture[size];

            for( int i = 0 ; i < size; i++)
            {
                m_VertexArray[i] = new VertexPositionTexture();
            }

            m_Size = size;
        }

        public VertexPositionTexture[] GetVertexArray()
        {
            return m_VertexArray.ToArray();
        }


        public Vector2 GetVertexPos2D(int index)
        {
            Vector2 v = new Vector2(m_VertexArray[index].Position.X, m_VertexArray[index].Position.Y);
            return v;
        }

        public void SetVertexPos2D(int index , Vector2 v)
        {
            Vector3 v3 = new Vector3(v.X, -v.Y, 0);
            m_VertexArray[index].Position = v3;
        }

        public Vector2 GetVertexTexCoord(int index )
        {
            return m_VertexArray[index].TextureCoordinate;
        }

        public void SetVertexTexCoord(int index , Vector2 v)
        {
            m_VertexArray[index].TextureCoordinate = v;
        }
        
        public void Release()
        {
            m_VertexArray = null;
        }

        public  void Draw(Texture2D texture , VertexPositionTexture[] vertice , int startVertex , int primitCount , RasterizerState rasterState)
        {
            GraphicShareData graphicData = GraphicShareData.GetShareData();
            GraphicsDevice graphicDevice = graphicData.GetGraphicDevice();
            BasicEffect basicEffect = graphicData.GetBasicEffect();

            RasterizerState prevState = graphicDevice.RasterizerState;

            graphicDevice.RasterizerState = rasterState;

            VertexBuffer vb = new VertexBuffer(graphicDevice, typeof(VertexPositionTexture), vertice.Length, BufferUsage.None);
            vb.SetData(vertice);


            basicEffect.World = Vertex2DPipeMan.GetRenderPipeMan().GetBaseWorldMat();
            basicEffect.View = Vertex2DPipeMan.GetRenderPipeMan().GetViewMat();
            basicEffect.Projection = Vertex2DPipeMan.GetRenderPipeMan().GetProjectionMat();
            basicEffect.Texture = texture;
            basicEffect.TextureEnabled = true;


            graphicDevice.SetVertexBuffer(vb);

            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphicDevice.DrawPrimitives(PrimitiveType.TriangleStrip, startVertex, primitCount);
            }

            graphicDevice.RasterizerState = prevState;
         //   basicEffect.VertexColorEnabled = true;
        }
    }
}
